package db;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTable;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;

import java.sql.SQLException;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class DBui {
	DefaultTableModel model;
	public JFrame frame;
	private JTable table;
	private DBconnector core;
	JComboBox comboBox;
	private JMenuBar menuBar;
	String TableName;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DBui window = new DBui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(new JFrame(), e.getLocalizedMessage());
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DBui() {
		initialize();
		core = new DBconnector(this);
		core.connect("Rhyvos", "Staffik1");
	}
	
	public void addColumn(String title){
		model.addColumn(title);
	}
	public void addRow(Vector v){
		model.addRow(v);
	}
	public void addTable(String name){
		comboBox.addItem(name);
	}

	public void clearTable(){
		table.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
				}
			));
		model= (DefaultTableModel) table.getModel();
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 377);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setBounds(10, 50, 178, 20);
		frame.getContentPane().add(comboBox);
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(10, 95, 414, 206);
		frame.getContentPane().add(scrollPane);
		
		table  = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
			}
		));
		table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
	        scrollPane.setViewportView(table);
	    model= (DefaultTableModel) table.getModel();
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				core.querry(comboBox.getSelectedItem().toString());
				TableName=comboBox.getSelectedItem().toString();
				
				
			}
		});
		btnOk.setBounds(199, 49, 89, 23);
		frame.getContentPane().add(btnOk);
		
		menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 434, 21);
		frame.getContentPane().add(menuBar);
		
		JMenu mnConnect = new JMenu("Connect");
		menuBar.add(mnConnect);
		
		JMenu mnInput = new JMenu("Input");
		mnInput.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Vector<String> v=new Vector();
				TableName=comboBox.getSelectedItem().toString();
				try {
					core.querry(comboBox.getSelectedItem().toString());
					for  (int i = 2; i<= core.resultSet.getMetaData().getColumnCount(); i++){
					      v.add(core.resultSet.getMetaData().getColumnName(i));
					    }
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(new JFrame(), e1.getLocalizedMessage());
				}
				DBInput temp=new DBInput(v,core,comboBox.getSelectedItem().toString());
			}
		});
		mnInput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		menuBar.add(mnInput);
		
		JMenu mnQuerry = new JMenu("Querry");
		menuBar.add(mnQuerry);
		
		JButton btnDelete = new JButton("delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int tab[]=table.getSelectedRows();
				for(int i=0;i<tab.length;i++)
					try {
						core.statement.executeUpdate("call delete"+TableName+"("+table.getModel().getValueAt(tab[i], 0).toString()+")");
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(new JFrame(), e1.getLocalizedMessage());
					}
			}
		});
		btnDelete.setBounds(10, 305, 89, 23);
		frame.getContentPane().add(btnDelete);
	}
}





